/**
 * Collaborative Story Development Web Application (CSDApp) 
 * Copyright Anton Strack 2014
 *
 * This file is part of Collaborative Story Development Application (CSDApp).
 *
 * CSDApp is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or any later version.
 *
 * CSDApp is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * CSDApp. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package csdwa;

import java.util.Collection;

/**
 * This class just checks that a value is not empty according to default empty
 * values.
 *
 * @author Anton Strack
 */
public class IsNotEmpty extends Validator {

    /**
     * Default constructor
     */
    public IsNotEmpty() {
        super("IsNotEmpty");
    }

    /**
     * Get the generic error message for this validator.
     *
     * @param validatorValueDisplayName the name of the value to display to an
     * end user when there is an error. For instance Email for an email
     * ValidatorValue object.
     * @return The error message of the validator
     */
    @Override
    public String getErrorMessage(String validatorValueDisplayName) {
        return validatorValueDisplayName + " cannot be empty.";
    }

    /**
     * This method will validate objects that are instantiated as Strings.
     *
     * @param value the value to validate
     * @return true if the value is valid
     */
    @Override
    protected boolean handleString(String value) {
        return !value.isEmpty(); // negate the result of isEmpty() so the the result is as expected with being empty equaiting to false
    }

    /**
     * This method will validate objects that are not Strings or Numbers.
     *
     * @param value the value to validate
     * @return true if the value is valid
     */
    @Override
    protected boolean handleObject(Object value) {
        if (value.getClass().isAssignableFrom(java.util.Collection.class)) {
            Collection<Object> values = (Collection<Object>) value;
            return !values.isEmpty();
        }
        return false;
    }

    /**
     * This method will validate objects that are instantiated as a class that
     * extends java.lang.Number.
     *
     * @param value the value to validate
     * @return true if the value is valid
     */
    @Override
    protected boolean handleNumber(Number value) {
        // assuming that 0 equals empty, otherwise number would never be empty since the default value is 0;
        // Check is empty with double to not lose values rigth of the decimal and thus say value is empty when it isn't.
        if (value.doubleValue() == 0.0) {
            return false;
        }
        return true;
    }
}
